<?php
/**
 * 会员管理
 */

namespace app\admin\controller;

use app\model\UserInfo as ModelUserInfo;
use app\model\CourierInfo as ModelCourierInfo;
use app\model\DriverInfo as ModelDriverInfo;
use app\model\StationInfo as ModelStationInfo;
use app\logic\Excel as MobileLogicExcel;

class User extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取会员管理信息
     * uri:/admin/user.info
     *
     * @return
     */

    public function MemberInformation(){

        //$type = $this->_getContentParams('type');


        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $arr_where = '';
        $str = '';


        $userId = $this->_getContentParams('userId');
        $nickName = $this->_getContentParams('nickName');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        $sort = $this->_getContentParams('sort');
        _toCompare(empty($sort),'==',true,20012);

        if(!empty($userId)){
            $arr_where.=" user_info.user_id like '%$userId%' or user_info.mobile like '%$userId%'";
            if (!empty($nickName) or !empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($nickName)){
            $arr_where.="$str user_info.nickname like '%$nickName%'";
            if (!empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($startTime)){
            $arr_where.="$str DATE_FORMAT(user_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
            if (!empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($endTime)){
            $arr_where.="$str DATE_FORMAT(user_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
        }

        $int_total_rows = ModelUserInfo::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

        $list = ModelUserInfo::getInstance()->getUserList($int_first_row,$int_list_rows,$arr_where,$sort);
        _returnData(['totalPages' => $int_total_pages,'userInfoList'=>$list]);

    }


    /**
     * 获取快递员、司机、站点、代理商管理首页信息
     * uri:/admin/user.information
     */
    public function Information(){
        $type = $this->_getContentParams('type');

        //$type = 3;

        $int_now_page = $this->_getContentParams('page'); // 当前页码
        if (empty($int_now_page)){
            $int_now_page = 1;
        }
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        if (empty($int_list_rows)){
            $int_list_rows = 10;
        }
        _toValidate('id', $int_list_rows, 10006);

        $sort = $this->_getContentParams('sort');
        _toCompare(empty($sort),'==',true,20012);

        $arr_where = '';
        $str = '';

        switch ($type){
            case 1:
                //$db_name = 'courier_info';//快递员信息表
                //$arr_where.='(courier_info.state=1 or courier_info.state=3)';
                $courierId = $this->_getContentParams('courierId');
                $nickName = $this->_getContentParams('nickName');
                $startTime = $this->_getContentParams('startTime');
                $endTime = $this->_getContentParams('endTime');
                if(!empty($courierId)){
                    $arr_where.=" (courier_info.id like '%$courierId%' or courier_info.mobile like '%$courierId%')";
                    if (!empty($nickName) or !empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($nickName)){
                    $arr_where.=" $str courier_info.real_name like '%$nickName%'";
                    if (!empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($startTime)){
                    $arr_where.=" $str DATE_FORMAT(courier_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
                    if (!empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($endTime)){
                    $arr_where.=" $str DATE_FORMAT(courier_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
                }

                $int_total_rows = ModelCourierInfo::getInstance()->toCount($arr_where);
                $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
                _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
                $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

                $list = ModelCourierInfo::getInstance()->getCourierInfo($arr_where,$int_first_row,$int_list_rows,$sort);
                _returnData(['totalPages' => $int_total_pages,'courierInfoList'=>$list]);
                break;
            case 2:
                //$db_name = 'driver_info';//司机信息表
                $driverId = $this->_getContentParams('driverId');
                $nickName = $this->_getContentParams('nickName');
                $startTime = $this->_getContentParams('startTime');
                $endTime = $this->_getContentParams('endTime');

                if(!empty($driverId)){
                    $arr_where.=" driver_info.id like '%$driverId%' or driver_info.mobile like '%$driverId%'";
                    if (!empty($nickName) or !empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($nickName)){
                    $arr_where.="$str driver_info.name like '%$nickName%'";
                    if (!empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($startTime)){
                    $arr_where.="$str DATE_FORMAT(driver_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
                    if (!empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($endTime)){
                    $arr_where.="$str DATE_FORMAT(driver_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
                }

                $int_total_rows = ModelDriverInfo::getInstance()->toCount($arr_where);
                $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
                _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
                $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

                $list = ModelDriverInfo::getInstance()->getDriverInfoList($arr_where,$int_first_row,$int_list_rows,$sort);
                _returnData(['totalPages' => $int_total_pages,'driverInfoList'=>$list]);
                break;
            case 3:
                //$db_name = 'station_info';//站点信息表
                $name = $this->_getContentParams('name');
                $level = $this->_getContentParams('level');
                $startTime = $this->_getContentParams('startTime');
                $endTime = $this->_getContentParams('endTime');

                if(!empty($name)){
                    $arr_where.=" station_info.name like '%$name%'";
                    if (!empty($level) or !empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($level)){
                    $arr_where.="$str station_info.level like '%$level%'";
                    if (!empty($startTime) or !empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($startTime)){
                    $arr_where.="$str DATE_FORMAT(station_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
                    if (!empty($endTime)){
                        $str=" and";
                    }
                }

                if (!empty($endTime)){
                    $arr_where.="$str DATE_FORMAT(station_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
                }

                $int_total_rows = ModelStationInfo::getInstance()->toCount($arr_where);
                $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
                _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
                $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

                $list = ModelStationInfo::getInstance()->getStationInfoList($arr_where,$int_first_row,$int_list_rows,$sort);
                _returnData(['totalPages' => $int_total_pages,'stationInfoList'=>$list]);
                break;
            default:
                break;
        }
    }

    /**
     * 用户状态修改
     * uri:/admin/user.state
     */
    public function userState(){
        $userId = $this->_getContentParams('userId');
        _toValidate('id',$userId,12089);
        $isEnable = $this->_getContentParams('isEnable');
        _toValidate('is_enable',$isEnable,10822);
        $arr_where=[
            'is_enable'=>$isEnable
        ];
        $data = ModelUserInfo::getInstance()->toUpdate("user_id=$userId",$arr_where);
        _returnData($data);
    }

    /**
     * 会员信息列表导出
     * uri:/admin/user.infoExport
     */
    public function userInfoExport(){
        $str_excel = $this->_getContentParams('excel');
        $arr_where = '';
        $str = '';


        $userId = $this->_getContentParams('userId');
        $nickName = $this->_getContentParams('nickName');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        $sort = $this->_getContentParams('sort');
        _toCompare(empty($sort),'==',true,20012);

        if(!empty($userId)){
            $arr_where.=" user_info.user_id like '%$userId%' or user_info.mobile like '%$userId%'";
            if (!empty($nickName) or !empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($nickName)){
            $arr_where.="$str user_info.nickname like '%$nickName%'";
            if (!empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($startTime)){
            $arr_where.="$str DATE_FORMAT(user_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
            if (!empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($endTime)){
            $arr_where.="$str DATE_FORMAT(user_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
        }
        $list = ModelUserInfo::getInstance()->getUserList('','',$arr_where,$sort);
        if($str_excel == 'excel'){
            MobileLogicExcel::getInstance()->export($list, ['userId' => '会员ID', 'mobile' => '用户账号', 'nickName' => '用户昵称', 'realName' => '真实姓名','cnt'=>'寄件总次数','amount'=>'寄件总消费（元）','gmtCreate'=>'注册时间','isEnable'=>'账户状态（0:禁用 1:启用）'], '会员管理会员信息'.date('Y-m-d', time()));
        }
        _returnData();
    }

    /**
     * 会员管理-》密码重置
     * uri:/admin/user.passwordReset
     */
    public function userPasswordReset(){
        $userId = $this->_getContentParams('userId');
        _toValidate('id',$userId,12089);
        $str_pwd_key = $this->_getRandomString(8);
        $password = passwordEncryption('123456', $str_pwd_key);
        $arr_where=[
            'password'=>$password,
            'pwd_key'=>$str_pwd_key
        ];
        $data = ModelUserInfo::getInstance()->toUpdate("user_id=$userId",$arr_where);
        if ($data>0){
            _returnData();
        }
    }
}